//
//  VisitorView.swift
//  晓庄微博
//
//  Created by Maxwell on 10/31/18.
//  Copyright © 2018 Maxwell. All rights reserved.
//

import UIKit
import SnapKit

//协议1.定义
protocol VisitorViewDelegate:NSObjectProtocol {
    //委托别人做什么
    func registerButtonWillClicked()
    func loginButtonWillClicked()
}

class VisitorView: UIView {
    //协议2. 定义一个属性，类型为协议
    weak var delegate:VisitorViewDelegate? //weak 防止循环引用
    private lazy var houseImageView:UIImageView = {
        let houseImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        
        return houseImageView
    }()
    
    private lazy var discoverImageView:UIImageView = {
        let discoverImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
        return discoverImageView
        
    }()
    
    private lazy var myLabel:UILabel = {
        let myLabel = UILabel()
        myLabel.text = "关注一些，发现一些有趣的事情"
        myLabel.textColor = UIColor.red
        myLabel.numberOfLines = 0
        myLabel.preferredMaxLayoutWidth = 300
        myLabel.textAlignment = .center
        return myLabel
    }()
    
    
    private lazy var registerButton:UIButton = {
        let registerButton = UIButton()
        registerButton.setTitle("注册", for: .normal)
        registerButton.setTitleColor(UIColor.black, for: .normal)
        registerButton.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        registerButton.addTarget(self, action: #selector(registerButtonClicked), for: .touchUpInside)
        return registerButton
    }()
    
    private lazy var loginButton:UIButton = {
        let loginButton = UIButton()
        loginButton.setTitle("登录", for: .normal)
        loginButton.setTitleColor(UIColor.black, for: .normal)
        loginButton.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        loginButton.addTarget(self, action: #selector(loginButtonClicked), for: .touchUpInside)
        return loginButton
    }()
    
    @objc func registerButtonClicked(){
        //希望能弹出一个登录的界面，这个工作是由controller做的，这个visitorview是在
        //basetableviewcontroller里面被创建的，发通知给控制器
        //协议3.在View里面调用协议的方法
        delegate?.registerButtonWillClicked()
    }
    
    @objc func loginButtonClicked(){
        //协议3.在View里面调用协议的方法
        delegate?.loginButtonWillClicked()
    }
    func setupInfo(isHome:Bool,imageName:String,message:String){
        discoverImageView.isHidden = !isHome
        houseImageView.image = UIImage(named: imageName)
        myLabel.text = message
    }
    
    override init(frame: CGRect) {
        super.init(frame:frame)
        backgroundColor = UIColor(white: 237.0/255, alpha: 1.0)

        //1.添加控件
        addSubview(houseImageView)
        addSubview(discoverImageView)
        addSubview(myLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        //2.布局控件
        houseImageView.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
        }
        discoverImageView.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
        }
      
        myLabel.snp.makeConstraints { (make) in
            make.top.equalTo(discoverImageView.snp.bottom).offset(10)
            make.centerX.equalTo(discoverImageView.snp.centerX)
        }
        
        registerButton.snp.makeConstraints { (make) in
            make.width.equalTo(100)
            make.top.equalTo(myLabel.snp.bottom).offset(10)
            make.centerX.equalToSuperview().offset(-60)
        }
        
        loginButton.snp.makeConstraints { (make) in
            make.width.equalTo(100)
            make.top.equalTo(myLabel.snp.bottom).offset(10)
            make.centerX.equalToSuperview().offset(60)
        }
        
    }
    
    //代码创建和图形创建只能选一个
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}
